"use strict";
/* Controllers */
app.controller("operationDashboardController", ['$scope', '$location', '$rootScope', '$routeParams', 'operationDashboardService', '$q', function ($scope, $location, $rootScope, $routeParams, operationDashboardService, $q) {
    var vm = $scope.vm = {};

    vm.p = {};
    vm.p.biz = 'zp';
    vm.p.appDiv = 'zhaopinzhe';

    var datePicker = $('.date');
    datePicker.datetimepicker({
        locale: 'zh-cn',
        format: 'YYYY-MM-DD'
    });
    datePicker.on('dp.hide', function (e) {
        var currentInput = $(e.currentTarget).find('input');
        var eName = currentInput.attr('name');
        var p = $scope.vm.p;
        p[eName] = currentInput.val();
        $scope.$apply()
    });

    if ($scope.vm.p.bizTime == '' || typeof ($scope.vm.p.bizTime) == 'undefined') {
        $scope.vm.p.bizTime = new Date(new Date().getTime() - 86400000).format('yyyy-MM-dd');
    }

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listCoreMetrics = function () {
        var params = {};
        params = vm.p;
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                alert("请求失败：" + response.message);
                return;
            }
            $scope.vm.metricList = data
        };
        return operationDashboardService.listCoreMetrics(params, procFn, errorFn);
    };


    $scope.listChartMetrics = function () {
        var params = {};
        params = vm.p;
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                alert("请求失败：" + response.message);
                return;
            }
            $scope.vm.allChartMetrics = data
        };
        return operationDashboardService.listChartMetrics(params, procFn, errorFn);
    };

    function initChart(dom, data, title) {

        var dateList = data.map(function (item) {
            return item[0];
        });
        var valueList = data.map(function (item) {
            return item[1];
        });
        // var dom = document.getElementById("container");
        var myChart = echarts.init(dom);
        window.onresize = function () {
            myChart.resize();
        };
        var option;

        option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: { type: 'cross' }
            },
            legend: {
            },
            dataZoom: [{}],
            xAxis: {
                type: 'category',
                data: dateList
            },
            yAxis: [
                {
                    name: '指标值',
                    type: 'value'
                }
            ],
            series: [
                {
                    name: title,
                    yAxisIndex: 0,
                    data: valueList,
                    type: 'line',
                    smooth: true
                }
            ]
        };
        window.onresize = function () {
            myChart.resize();
        };
        if (option && typeof option === 'object') {
            myChart.setOption(option);
        }
    }

    $scope.repeatFinish = function () {
        $scope.$apply();
        var jqDataChart = $('#dapan_charts').find('div[data-chart-key]');
        $.each(jqDataChart, function (i, dom) {
            var metaKey = $(dom).attr('data-chart-key');
            var chartEntity = $scope.vm.allChartMetrics.find(function (x) {
                return x.metaId == metaKey;
            });
            initChart(dom, chartEntity.chartData, chartEntity.indexTitle)
        })
    };

    $scope.listDashboardDimData = function () {
        var params = {};
        params = vm.p;
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                alert("请求失败：" + response.message);
                return;
            }
            $scope.vm.dashboardDimData = data
        };
        return operationDashboardService.listDashboardDimData(params, procFn, errorFn);
    };
    //
    //$scope.initTables = function () {
    //    $scope.$apply();
    //    var dashboardDimData = $scope.vm.dashboardDimData;
    //    for(var i in dashboardDimData) {
    //        var item = dashboardDimData[i];
    //        var columns = [];
    //        for(var k in item.columnNames) {
    //            var element = item.columnNames[k];
    //            var column = {};
    //            column.title = element;
    //            column.field = element;
    //            column.sortable = true;
    //            columns.push(column)
    //        }
    //        columns[0].width=100;
    //        var tableData = item.tableData;
    //        var $table = $("#list_table_" + item.configId);
    //        $table.bootstrapTable('destroy').bootstrapTable({
    //            columns: columns,
    //            data: tableData
    //        })
    //    }
    //};

    $scope.queryDashboard = function () {
        //$("#refreshTable").addClass('disabled');
        //$scope.listDashboardDimData();
        $q.all([$scope.listCoreMetrics(), $scope.listChartMetrics()]).then(function () {
            $("#refreshTable").removeClass('disabled');
        })
    };

    $scope.queryDashboard()

}]);

//自定义指令实现ng-repeat完成后执行repeatFinish方法
//app.directive('myRepeatFinish', function () {
//    return {
//        restrict: 'A',
//        link: function (scope, element, attr) {
//            console.log(scope.$index);
//            if (scope.$last == true) {
//                console.log('ng-repeat over, will draw chart.');
//                scope.$eval(attr.myRepeatFinish)
//            }
//        }
//    }
//});

/* Services */
app.service('operationDashboardService', ['ajaxService', function (ajaxService) {

    this.listCoreMetrics = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/web_dapan/listCoreMetrics.do", successFunction, errorFunction);
    };
    this.listChartMetrics = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/web_dapan/listAllMetricSeries.do", successFunction, errorFunction);
    };
    this.listDashboardDimData = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/web_dapan/listDashboardDimData.do", successFunction, errorFunction);
    };

}]);

